package com.zjj.day0504.book;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date 2025/5/4
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class BookManagementSystem {
    private static  List<Book> bookList = new ArrayList<>();
    private static  Scanner scanner = new Scanner(System.in);

    static {
        bookList.add(new Book("名著", "西游记", 19.0));
        bookList.add(new Book("名著", "水浒传", 29.0));
        bookList.add(new Book("it书籍", "Java入门到精通", 99.0));
        bookList.add(new Book("it书籍", "PHP入门到精通", 9.9));
    }

    public static void Menu() {
        System.out.println("--------欢迎来到学生管理系统--------");
        System.out.println("1. 查看所有书籍");
        System.out.println("2. 添加书籍");
        System.out.println("3. 删除书籍");
        System.out.println("4. 修改书籍");
        System.out.println("5. 退出系统");
        System.out.println("请输入你的选择：");
    }

    public static void Books() {
        for (Book book : bookList) {
            System.out.println(book);
        }
    }

    public static void addBook() {
        System.out.println("请输入要添加书籍的类型:");
        String type = scanner.nextLine();
        System.out.println("请输入要添加的书名:");
        String name = scanner.nextLine();
        System.out.println("请输入要添加书的价格:");
        double price = scanner.nextDouble();
        Book newBook = new Book(type, name, price);
        bookList.add(newBook);
        System.out.println("添加" + name + "成功");
    }

    public static void deleteBook() {
        System.out.println("请输入要删除书籍的类型:");
        String type = scanner.nextLine();
        System.out.println("请输入要删除的书名:");
        String name = scanner.nextLine();
        for (int i = 0; i < bookList.size(); i++) {
            if (bookList.get(i).getType().equals(type) && bookList.get(i).getName().equals(name)) {
                bookList.remove(i);
                System.out.println("删除" + name + "书籍成功");
                return;
            }
        }
        System.out.println("未找到该图书，删除失败!");
    }

    public static void updateBook() {
        System.out.println("请输入要修改书籍的类型:");
        String type = scanner.nextLine();
        System.out.println("请输入要修改的书名:");
        String name = scanner.nextLine();
        for (Book book : bookList) {
            if (book.getType().equals(type) && book.getName().equals(name)) {
                System.out.println("请输入新的图书类型:");
                String newType = scanner.nextLine();
                book.setType(newType);
                System.out.println("请输入新的书名:");
                String newName = scanner.nextLine();
                book.setName(newName);
                System.out.println("请输入新的价格:");
                Double newPrice = Double.valueOf(scanner.nextLine());
                book.setPrice(newPrice);
                System.out.println("图书修改成功!");
                return;
            }
        }
        System.out.println("未找到该图书，修改失败!");
    }

    public static void main(String[] args) {
        while (true) {
            Menu();
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    Books();
                    break;
                case 2:
                    addBook();
                    break;
                case 3:
                    deleteBook();
                    break;
                case 4:
                    updateBook();
                    break;
                case 5:
                    System.out.println("谢谢你的使用");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入!");
            }
        }
    }
}